<?
include(DOCINCLUDES."global_files/global_locale.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/report_php.inc");
//include(DOCINCLUDES."global_files/tables.inc");
// include(DOCINCLUDES."global_files/global_session.inc");

include(DOCINCLUDES."global_files/form_php.inc");
$db   = new cDatabase();
$page = new cPage();

$company_id="_100350";
$table_view_name="move_to_stock";
$report_code="mvtostk";
$report_name="Move To Stock Report";
// class createscript {
// function createcolumns($report_code,$report_name)
// {
$date_val=date("dmy");
$ourFileName =$date_val.implode("_",explode(" ",$report_name)).".php";
$handle = fopen($ourFileName, 'w');
 fwrite($handle, $somecontent);
fclose($handle);

$handle = fopen($ourFileName, 'a');//fopen($ourFileName, 'a') or die("can't open file");

$reportdetailscontent='<?php'." \n".' include ("global_files/report_customization.inc");'." \n".'$report_code="'.strtolower($report_code).'";'." \n".'
$report_name="'.ucfirst($report_name).'";'."\n".'
$view_name="'.ucfirst($table_view_name).'";'."\n\n\n";

 $reportsql ='$db->query="begin work";'."\n".' $db->runQuery();'."\n".'createNewReport($report_code, $report_name,$view_name);'."\n".'InsertNewColumns($report_code,$columns_array1,$columns_array2);'."\n".'$db->query="commit work";'."\n".'$db->runQuery();'."\n \n ?>";

  fwrite($handle, $reportdetailscontent);

 echo $db->query="SELECT column_name,data_type from information_schema.columns where table_schema ='$company_id' and table_name='$table_view_name' ;";
$db->runQuery();
$column_names_type_array=$db->returnArrays();
  $val=0;
  $display_order=1;
   $line0='$i ='. $val. " ;\n";
  fwrite($handle, $line0);
 foreach($column_names_type_array as $key=>$value)
 {
if($value["data_type"]=="date")
{
$line1='$columns_array1[$i]["db_column_name"]="to_char('.$value["column_name"].',\'\'dd/mm/yyyy\'\')";'."\n";
}
else
{
$line1= '$columns_array1[$i]["db_column_name"]="'.$value["column_name"]."\"; \n";
}
$line2='$columns_array1[$i]["script_variable_name"]="'.$value["column_name"]."\";\n";
$line3='$columns_array1[$i]["privilege_id"]="1";'."\n";
$line4='$columns_array1[$i]["permission"]="r";'."\n";
$line5='$columns_array1[$i]["fetch_column"]="t";'."\n\n";
$line6='$columns_array2[$i]["display_name"]="'.ucfirst(implode(" ",explode("_",$value["column_name"])))."\";\n";
$line7='$columns_array2[$i]["display_order"]="'.$display_order."\";\n";
$line8='$columns_array2[$i]["set_default"]="t";'."\n" ;

$line9='$i ++;'."\n\n\n";
fwrite($handle, $line1);
fwrite($handle, $line2);
fwrite($handle, $line3);
fwrite($handle, $line4);
fwrite($handle, $line5);
fwrite($handle, $line6);
fwrite($handle, $line7);

if($value["data_type"]=="integer"||$value["data_type"]=="numeric" )
{
$line_total0='$columns_array2[$i]["display_summary"]="t";'."\n";
$line_total1='$columns_array2[$i]["decimal_places"]="2";'."\n";
fwrite($handle, $line_total0);
fwrite($handle, $line_total1);
}
fwrite($handle, $line8);
fwrite($handle, $line9);
$val++;
$display_order++;

}


 fwrite($handle, $reportsql);

fclose($handle);

$filterFileName=$date_val.implode("_",explode(" ",$report_name))."_filters.php";

$handle = fopen($filterFileName, 'w');
fwrite($handle, $somecontent1);
fclose($handle);

$handle1 = fopen($filterFileName, 'a') ; //or die("can't open file");
$filter_file_line0="<?php \n session_start(); \n include (\"global_files/report_customization.inc\"); \n include (\"global_files/tables.inc\"); \n\n \$i=0; \n ";
fwrite($handle1, $filter_file_line0);

$db->query="SELECT column_name,data_type from information_schema.columns where table_schema ='$company_id' and table_name='$table_view_name' ;";
$db->runQuery();
$column_names_type_array=$db->returnArrays();
 foreach($column_names_type_array as $key=>$value)
 {
$filter_file_line1='$columns_array1[$i]["filter_name"]="'.$value["column_name"].'";'."\n";
$filter_file_line2='$columns_array1[$i]["report_code"]="'.$report_code.'";'."\n";
$filter_file_line3='$columns_array1[$i]["script_variable_name"]="'.$value["column_name"].'";'."\n";
$filter_file_line4='$columns_array1[$i]["group_column"]="";'."\n";
$filter_file_line5='$columns_array1[$i]["set_default"]="t";'."\n";
$filter_file_line6='$i++;'."\n\n";

fwrite($handle1, $filter_file_line1);
fwrite($handle1, $filter_file_line2);
fwrite($handle1, $filter_file_line3);
fwrite($handle1, $filter_file_line4);
fwrite($handle1, $filter_file_line5);
fwrite($handle1, $filter_file_line6);

}
$filter_file_line7="\$db->query=\"begin work;\"; \n \$db->runQuery(); \n insertNewReportFilters(\$columns_array1); \n \$db->query=\"commit work;\"; \n \$db->runQuery(); \n ?>";

fwrite($handle1, $filter_file_line7);

fclose($handle1);
$masterfilterFileName=$date_val."master_filter".implode("_",explode(" ",$report_name)).".php";
$handle2 = fopen($masterfilterFileName, 'w');
fwrite($handle2, $somecontent1);
fclose($handle2);

$handle2 = fopen($masterfilterFileName, 'a') ; //or die("can't open file");
$masterfilter_line1="<?php \n \n include (\"global_files/report_customization.inc\"); \n include (\"global_files/tables.inc\"); \n\n \$i=0; \n";

fwrite($handle2, $masterfilter_line1);
$db->query="SELECT column_name,data_type from information_schema.columns where table_schema ='$company_id' and table_name='$table_view_name' ;";
$db->runQuery();
$column_names_type_array=$db->returnArrays();
 foreach($column_names_type_array as $key=>$value)
{
$masterfilter_line2='$columns_array1[$i]["filter_name"]="'.$value["column_name"].'";'."\n";
$masterfilter_line3='$columns_array1[$i]["filter_table_join"]="'.$table_view_name.'";'."\n";
$masterfilter_line4='$columns_array1[$i]["form_field_name[1]"]="distinct('.$value["column_name"].')";'."\n";
$masterfilter_line5='$columns_array1[$i]["form_display"]="'.ucfirst(implode(" ",explode("_",$value["column_name"]))).'";'."\n";
$masterfilter_line6='$columns_array1[$i]["fetch_condition"]="";'."\n";
$masterfilter_line7='$columns_array1[$i]["order_by"]="1";'."\n";
$masterfilter_line8='$i++;'."\n\n";

fwrite($handle2, $masterfilter_line2);
fwrite($handle2, $masterfilter_line3);
fwrite($handle2, $masterfilter_line4);
fwrite($handle2, $masterfilter_line5);
fwrite($handle2, $masterfilter_line6);
fwrite($handle2, $masterfilter_line7);
fwrite($handle2, $masterfilter_line8);
}

$masterfilter_line9="\$db->query=\"begin work;\";\n\$db->runQuery();\ncreateNewFilters(\$columns_array1);\n\$db->query=\"commit work;\";\n\$db->runQuery();\n\n?>";
fwrite($handle2, $masterfilter_line9);
fclose($handle2);
// }
// }foreach(\$columns_array1[\$i] as \$key=>\$value) \n {\n\t\$columns_array1[\$i][\$key]=trim(\$value);\n}\n


?>
